﻿using UnityEngine;
using System.Collections;

public class DummyNinjaHandler : MonoBehaviour {

	public GameObject Model;
	// private VisionControl _visionControl;
	private Animator _animator;
	private Vector2 _movement;
	private Vector2 _currentPos, _previousPos;
	private bool _activeBefore;
	
	// Use this for initialization
	void Start () {
		// _visionControl = GetComponentInChildren<VisionControl>();
		_animator = Model.GetComponent<Animator>();
		_currentPos = Vector2.zero;
		_activeBefore = false;
	}

	// Update is called once per frame
	void Update () {
		_previousPos = _currentPos;
		_currentPos = Model.renderer.transform.position;
		
		_movement = _currentPos - _previousPos;
		
		if (_movement != Vector2.zero) {
			// Movement on the x-axis is happening! WE GOT A DIRECTION OF MOVEMENT
			_activeBefore = true;
			_animator.Play("walk");
			
			var facing = Model.transform.localScale;
			var x = Mathf.Abs(facing.x);
			facing.x = _movement.x > 0 ? -x : x;
			Model.transform.localScale = facing;
			
			// var deg = Mathf.Lerp(180 * Mathf.Sign(_movement.y), 0, Mathf.InverseLerp(-1, 1, _movement.x));
			// _visionControl.Rotate(deg);		
		} else if (_activeBefore) {
			// Start the idle-animation, if it was active before.
			_activeBefore = false;
			_animator.Play("idle");
		}
	}
}
